package com.hf.action;


import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Query;

import tool.TimeTool;
import tool.UploadImg;
import com.base.bean.*;
import com.base.dao.*;
import com.base.service.*;
import com.base.domain.*;

/**
 * 恒安行动管理
 * 
 * 比销售员的简单，因为不会选择客户基本都是自己去填
 * 
 * 添加照片
 * ajax 分页？
 * 
 * 
 * Image image=ImageService.getInstance().saveImageInfo(creator, mediaId, longitude, latitude,2);
 * 
 * HaActivity表中新增了paymemo字段作为还款描述字段
 * @author Administrator
 *
 */

public class ActvtHaAction extends BaseAction{
	static String[] actvType = {"", "催收", "执行", "诉讼"};
	
	private Integer type,oppType;	//行动类型
	private String longitude,latitude,memo,name;
	private String paymemo; //催收信息
	private String imgId;	//上传到服务器的image的id
	
	private Integer flag;	//a:show  b:提交
	

	
	private List<ActivityHa> dataList;	
	
	
	public ActvtHaAction() {

	}


	
	
	/**
	 * 应该有两个状态，flag：0   1:提交态
	 */

	@Override
	public String execute() {
		
//		测试赋值
//		this.openId="olWUvt186RJV4_obaeMV5SGT4e2g";
//		this.imgId="dOPVnaoJ5bPhtmNLGW9YqtHAyward5gehriOP4yP3JAUOquXRzu3DuQPjJzOak3z";
//		this.cid=117;		
//		this.longitude="104.1674";
//		this.latitude="30.71578";
//		this.memo="";	
		this.flag = (this.flag==null?0:this.flag);
		this.tip="";
		this.openId=(this.openId==null?"":this.openId);
		this.type=(this.type==null?0:this.type);
		this.longitude=(this.longitude==null?"":this.longitude);
		this.latitude=(this.latitude==null?"":this.latitude);
		this.imgId=(this.imgId==null?"":this.imgId);
		this.name=(this.name==null?"":this.name);
		this.memo = (this.memo == null ? "" : this.memo.replaceAll("\\s*", ""));
		this.paymemo = (this.paymemo == null ? "" : this.paymemo.replaceAll("\\s*", ""));
		
		//System.out.println(this.state+"#####");
		
		if(this.openId.equals("")){		//这种情况一般不会出现
			this.tip="未识别到该用户";
			return "input";
		}
		
		User user=UserService.getInstance().getByOpenId(this.openId);
		
		if(user==null){					//这种情况一般不会出现
			this.tip="该用户还未注册";
			return ERROR;
		}
		

		
		
		
		boolean isShow=this.show(user.getUid(),20);	// 如果提交成功 则重新取最近5条行动记录
		if(!isShow){
			this.tip="显示最近20条行动记录失败(code=003)";
			return ERROR;
		}
		
		
		
//		if(this.state.equals("") && this.type!=9){	//except phoneHaActivity
//			this.tip="请完善行动内容(行动类型|位置信息|照片|备注)";
//			
//			isShow=this.show(user.getUid(),20);	// 如果提交成功 则重新取最近5条行动记录
//			if(!isShow){
//				
//				return ERROR;
//			}
//			return SUCCESS;
//		}
		
//		this.type=(this.type==null?0:this.type);	// repeat statement
		
		
		String tiXing="";
		
		if(this.flag.equals("b")){					//如果是提交状态
		
			if(this.type==0)
				tiXing=tiXing+" 行动类型";
			if(this.type!=9){		// except phoneHaActivity
				if(this.latitude.equals("") || this.longitude.equals(""))
					tiXing=tiXing+" 位置信息";
				if(this.imgId.equals(""))
					tiXing=tiXing+" 上传照片";
			}
			
			
			if(this.memo.equals(""))
				tiXing=tiXing+" 备注信息";
			
			if(!tiXing.equals("")){
				this.tip="创建失败！请完善行动内容：("+tiXing+" )";
				return ERROR;
			}
		
		
		
		ActivityHa activityHa=new ActivityHa();
		activityHa.setMemo(this.memo);
		activityHa.setLatitude(this.latitude);
		activityHa.setLongitude(this.longitude);
		activityHa.setCreator(user.getUid());
		activityHa.setType(this.type);
		
		if(this.type!=9){	// not phone HaActivity
			Image image=null;
			try{
			image=ImageService.getInstance().saveImageInfo(user.getUid(), this.imgId, this.longitude, this.latitude,2);
			activityHa.setPath(image.getPath());
			activityHa.setCreated(image.getCreated());
			activityHa.setPhotoName(image.getFileName());
			activityHa.setAddress(image.getDiscription());
			}catch(Exception e){
				e.printStackTrace();
				this.tip="提交行动照片失败(code=002)";
				return ERROR;
			}
		}else
			activityHa.setCreated(TimeTool.getCurrentTime());
		
		
//		HaActivity=HaActivityService.getInstance().creatActvt(user.getUid(),imgId,this.longitude,this.latitude);	//提交行动 包含了下载图片以及打水印
		
		try{
		ActivityHaService.getInstance().getActvtHaDao().merge(activityHa);
		
		isShow=this.show(user.getUid(),20);	// 如果提交成功 则重新取最近5条行动记录
		if(!isShow){
			
			return ERROR;
		}
		
		this.tip="提交行动成功";

		}catch(Exception e){
			e.printStackTrace();
			this.tip="提交行动失败(code=001)";
			return ERROR;
		}

		
		return SUCCESS;
		
		}
		return SUCCESS;
	}

	
	private boolean show(Integer creator,int maxCount){
		try{
			
			
			String begin=TimeTool.getTodayBeginStr();
			dataList=ActivityHaService.getInstance().getActvtHaDao().findByPage("from HaActivity a where a.creator="+creator+" order by created desc",0,maxCount);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
			return true;
		}
	
	

//	public static void main(String[] args){
//		HaActvtAction a=new HaActvtAction();
//		a.execute();
//		System.out.println(a.getTip());
//		System.out.println(a.getActvtList().get(0).getPhotoName());
//	}
	

	

	
	
	

}
